
package testeinterface;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class PCurso extends JPanel{       
        
        public PCurso(){            
            this.setLayout(new GridLayout(3,2));
            
            final JTextField tfCodigo = new JTextField("",20);
            final JTextField tfNome   = new JTextField("",20);
            JButton  btInserir   = new JButton("Inserir");
            JLabel lbCodigo = new JLabel("Código do curso");
            JLabel lbNome   = new JLabel("Nome do curso");
            btInserir      = new JButton("Inserir");
            
            btInserir.addActionListener (new ActionListener () {  

                @Override
                 public void  actionPerformed(ActionEvent e){

                     
                    Logger.gravar(Logger.INICIALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                     
                    try
                    {
                        Class.forName("org.apache.derby.jdbc.ClientDriver");
                    }
                    catch (Exception except) {
                        except.printStackTrace();
                    }

                     try{
                        String host = "jdbc:mysql://143.106.150.216:3306/SatodaDB?zeroDateTimeBehavior=convertToNull";
                        String uName = "satoda";
                        String uPass = "adotas2014";
                        
                        Connection con = DriverManager.getConnection(host, uName, uPass);
                        
                        Statement stmt = con.createStatement( );
                        String SQL = "INSERT INTO T_CURSO(codigo, nome) VALUES ("+ tfCodigo.getText() +", '"+ tfNome.getText() +"')";
                        stmt.executeUpdate( SQL );
                         System.out.println("Curso cadastrado com sucesso!");  
                    }
                    catch( SQLException err ){
                        //Logger.gravar("Erro durante a inserção no banco de dados na classe " + getNomeClasse() + ": " + err.getMessage());
                        Logger.gravar(Logger.ERRO, getNomeClasse() + ".actionPerformed(ActionEvent e): " + err.getMessage());
                        System.out.println(err.getMessage());
                    }
                     Logger.gravar(Logger.FINALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                 }  
              });  
            //jdksadskj
            
            this.add(lbCodigo);
            this.add(tfCodigo);
            
            this.add(lbNome);
            this.add(tfNome);
            
            this.add(btInserir);
        }
        
        
        private String getNomeClasse(){
            return this.getClass().getName();
        }
}

